
<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>会员充值</el-breadcrumb-item>
        <el-breadcrumb-item>限时特惠配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot-scope="scope" slot="timeForm">
          <div class="time">
            <div style="display:none">
              {{form.hours == null && scope.row.time && (form.hours = parseInt(scope.row.time / 3600))}}
              {{form.minutes == null && scope.row.time && (form.minutes = parseInt((scope.row.time - parseInt(scope.row.time / 3600) * 3600) / 60))}}
            </div>
            <el-input :disabled="timedisable" :value="form.hours" @input="hoursinput"></el-input><div class="timetext">小时</div>
            <el-input :disabled="timedisable" :value="form.minutes" @input="minutesinput"></el-input><div class="timetext">分钟</div>
          </div>
        </template>
        <template slot="is_online" slot-scope="scope">
          {{ scope.row.is_online == 1 ? '上线':'下线' }}
        </template>
        <template slot-scope="scope" slot="time">
          {{ parseInt(scope.row.time / 3600) == 0? '':  parseInt(scope.row.time / 3600) + '小时'}}{{parseInt((scope.row.time - parseInt(scope.row.time / 3600) * 3600) / 60)}}分钟      
        </template>
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>

        <!-- <template slot-scope="scope" slot="menu">
          <el-button @click="lineclick(scope.row)" type="text" size="small">{{ scope.row.is_online == 1 ? '下线':'上线' }}</el-button>
        </template> -->
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appConfigDiscount as baseURL,selectVersion,getGroupList } from "@/api/cat/baseUrls";
export default {
  data() {
    // 表单小时分钟的验证
    var validatePass = (rule, value, callback)=>{
      console.log(this.form.hours,'h')
      if (!this.form.hours) {
        callback(new Error('请输入小时'));
        
      } if (!this.form.minutes) {
        callback(new Error('请输入分钟'));
        
      }else {
        callback();
      }
    };
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {
        minutes:null,
        hours:null
      },
      timedisable:false,
      default: {
        orderByList: "create_time&DESC",
      },
      option: { 
        align:'center',
        menuAlign:'center',
        editBtn: true,
        viewBtn: false,
        addBtn: true,
        delBtn: true,
        searchBtn: true,
        refreshBtn: true,
        excelBtn: false,
        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "select",
            search: true,
            span:12,
            placeholder: "请输入APP版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
           {
            label: "用户群组",
            prop: "group_id",
            type: "select",
            placeholder: "用户属性",
            dicUrl: `${catUrl}/${getGroupList.getGroupList}`,
            props: {
              label: "group_name",
              value: "group_id",
            },
            rules: [
              {
                required: true,
                message: "用户群组必须选择",
              },
            ],
            dicFormatter: (res) => {
              console.log(res)
              return res.source;
            },
          },
           {
            label: "实付价格(元)",
            prop: "price",
            type: "input",
            span:12,
            placeholder: "请输入实付价格",
            labelWidth: '25%',
            rules: [
              {
                required: true,
                message: "实付价格必须填写",
                trigger: "blur"
              },
            ],
          },
         
          {
            label: "划线价格(元)",
            prop: "line_price",
            type: "input",
            labelWidth: '25%',
            placeholder: "请输入划线价格",
            span:12,
            rules: [
              {
                required: true,
                message: "划线价格必须填写",
              },
            ],
          },
          {
            label: "金币",
            prop: "coin",
            type: "number",
            placeholder: "请输入金币",
            span:12,
            rules: [
              {
                required: true,
                message: "金币必须填写",
              },
            ],
          },
          {
            label: "赠币",
            prop: "give_coin",
            type: "number",
            placeholder: "请输入赠币",
            span:12,
            rules: [
              {
                required: true,
                message: "赠币必须填写",
              },
            ],
          },
          {
            label: "倒计时",
            prop: "time",
            slot: true,
            formslot: true,
            span: 10,
            // rules: [{ required: true,validator: validatePass, trigger: 'blur' }]
          },
            {
            label: "状态",
            prop: "is_online",
            type: "select",
            placeholder: "请选择状态",
            slot: true,
            dicData: [
              {
                label: "下线",
                value: 0,
              },
              {
                label: "上线",
                value: 1,
              }
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "状态必须选择",
              },
            ]
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {

    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );

      this.data = response.data.map(item=>{
        item.price = item.price + ''
        item.line_price = item.line_price + ''
        return item
      })
      this.page= response.page;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done,loading) {
      this.timedisable = true
      row.time = Number(this.form.hours) * 3600 + Number(this.form.minutes) * 60
      delete row["id"];
      delete row["hours"];
      delete row["minutes"];
      row.create_time = new Date();
      let userInfo = this.$store.state.user.userInfo
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      let data = await common(baseURL.save, row, null, "POST");
      if (data==null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      row.time = Number(this.form.hours) * 3600 + Number(this.form.minutes) * 60
      delete row["hours"];
      delete row["minutes"];
      let userInfo = this.$store.state.user.userInfo
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      let data = await common(baseURL.update, row, null, "POST");
      if (data==null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
    
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {

      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    
    // 小时和分钟表单输入事件
    hoursinput(e){
      if(!e){
        e = ''
      }
      let _this =this
      _this.$nextTick(() => {
        _this.$set(this.form, "hours", e);
        _this.$forceUpdate();
      });
    },
    minutesinput(e){
      if(!e){
        e = ''
      }
      let _this =this
      _this.$nextTick(() => {
        _this.$set(this.form, "minutes", e);
        _this.$forceUpdate();
      });
    },

    // 上下线
    // lineclick(row){
    //   const { id, app_version, group_id } = row
      
    //   const msg = `确认要${row.is_online == 1? "下线":"上线"}吗？`
    //   this.$confirm(msg, "提示", {
    //     type: "warning",
    //   })
    //     .then(async () => {
          
    //       let response = await common(
    //         baseURL.updateState ,
    //         { id: id, version: app_version, groupId:group_id  },
    //         null,
    //         "GET"
    //       );
    //     })
    //     .then(() => {
    //       this.getList();
    //     });
    // }
  },
};
</script>

<style scoped>
.time {
  display: flex;
}
.timetext {
  width: 100px;
  padding: 0 10px 0 5px;
}
</style>